/*-------------------------------------------------------------------------
***************** Modification History ***********************************
   @doc
   @module clrkrpts.c - Clerk related Report functions |
   @end

$Log: /SoftPay/SPK2SIM/Source/Clerk/clrkrpts.c $
**************************************************************************

   Copyright (C) 1999 by VeriFone, Inc.

   All rights reserved.  No part of this software may be reproduced,
   transmitted, transcribed, stored in a retrieval system, or translated
   into any language or computer language, in any form or by any means,
   electronic, mechanical, magnetic, optical, chemical, manual or otherwise,
   without the prior written permission of VeriFone, Inc.

---------------------------------------------------------------------------*/
#include <project.h>


#ifdef  CLERK_DEFINED


#ifndef CLRKRPTS_C
#define CLRKRPTS_C
#endif

#include <stdlib.h>
#include <stdio.h>
#include <applidl.h>
#include <aclconio.h>
#include <aclstr.h>
#include <svctxo.h>
#include <dbmgr.h>      /* pj1 */
#include <formater.h>
#include <string.h>
#include <actutil.h>



/** /#define DEBUG_VSREPORT*/
#ifdef DEBUG_VSREPORT
#define DEBUG_95
#endif

#include <define.h>
#include <transact.h>
#include <settle.h>
#include <report.h>
#include <clrkrpts.h>
#include <sizes.h>
#include <review.h>
#include <bdt.h>
#include <cdt.h>
#include <cst.h>
#include <tblutil.h>
#include <hdt.h>
#include <tblutil.h>
#include <tct.h>
#include <pct.h>
#include <iit.h>
#include <mdlfuncs.h>
#include <merch.h>
#include <msg.h>
#include <msgfuncs.h>
#include <msgclrk.h>
#include <tran.h>
#include <ui.h>
#include <entry.h>
#include <file.h>
#include <form.h>
#include <pform.h>
#include <form2.h>
#include <clrkform.h>
#include <date.h>
#include <settlbat.h>
#include <pbatch.h>
#include <accum.h>
#include <spdebug.h>
#include <print.h>
#include <sdt.h>
#include <lct.h>
#include <pscalls.h>
#include <form.h>
#include <format.h>
#include <mit.h>
#include <codesec.h>
#include <clerk.h>
#include <trt.h>
#include <tip.h>
#include <amount.h>
#include <confio.h>
#include <udt.h>
#include <tblutil.h>

#define KEY_ONERPT  1
#define KEY_ALLRPT  2
/*********************************************************************************************
* @func int |inClerkTotalsReportOperation |
*
*  Initiates printing of the clerk totals report
*
*
*
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ERR
*
*
* @end
**********************************************************************************************/


int inClerkTotalsReportOperation(TRANSACTION_OBJECT* pobTran) {
    /*    pdebug(("--inClerkTotalsReportOperation--"));*/

    if (inGetPrinterType() == NOPRINTER && fGetEnablePrinter()) {
        vdDisplayMessageFromFile(NO_PRINTER_MSG, WARNING_MSG);
        return(VS_SUCCESS);
    }
    if (!fGetEnablePrinter())
        return(VS_SUCCESS);
    if (inGetClerkMode() != NO_CLERK) {
        inGetClerkTemplate();

        return(inPrintClerkTotalsReport(pobTran, &obPrinter));
    }

    vdDisplayMessageFromFile(TRANS_NOT_ALLWD_ERR_MSG, ERROR_MSG);
    return(VS_ERR);
}


/*********************************************************************************************
* @func int |inClerkDetailReportOperation |
*
*  Initiates printing of the clerk detail report
*
*
*
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ERR
*
*
* @end
**********************************************************************************************/


int inClerkDetailReportOperation(TRANSACTION_OBJECT* pobTran) {
    /*    pdebug(("--inClerkDetailReportOperation--"));*/

    if (inGetPrinterType() == NOPRINTER && fGetEnablePrinter()) {
        vdDisplayMessageFromFile(NO_PRINTER_MSG, WARNING_MSG);
        return(VS_SUCCESS);
    }

    if (!fGetEnablePrinter())
        return(VS_SUCCESS);

    if (inGetClerkMode() == NO_CLERK) {
        vdDisplayMessageFromFile(TRANS_NOT_ALLWD_ERR_MSG, ERROR_MSG);
        return(VS_ERR);
    }

    if (inGetPrinterType() != NOPRINTER) {
        inGetClerkTemplate();


        inPrintClerkDetailReport(pobTran, &obPrinter);

        return (VS_SUCCESS);
    }
    return(VS_SUCCESS);
}


/*********************************************************************************************
* @func int |inClerkTableReportOperation |
*
*  Initiates printing of the clerk table report
*
*
*
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ERR
*
*
* @end
**********************************************************************************************/


int inClerkTableReportOperation(TRANSACTION_OBJECT* pobTran) {
    /*    pdebug(("--inClerkTableReportOperation--"));*/

    if (inGetPrinterType() == NOPRINTER && fGetEnablePrinter()) {
        vdDisplayMessageFromFile(NO_PRINTER_MSG, WARNING_MSG);
        return(VS_SUCCESS);
    }
    if (!fGetEnablePrinter())
        return(VS_SUCCESS);

    if (inGetClerkMode() != NO_CLERK) {
        inGetClerkTemplate();

        return(inPrintClerkTableReport(&obPrinter, pobTran));
    }

    vdDisplayMessageFromFile(TRANS_NOT_ALLWD_ERR_MSG, ERROR_MSG);
    return(VS_ERR);
}



/*********************************************************************************************
* @func int |inClerkReviewOperation |
*
*  Allows browsing of the clerk table at the terminal
*
*
*
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ERR
*
*
* @end
**********************************************************************************************/


int inClerkReviewOperation(void) {
    /*    pdebug(("--inClerkReviewOperation--"));*/

    return (inConfRecEdit("UDT"));
}



/*********************************************************************************************
* @func int |vdSetClerkTotalHeaderCondition |
*
*  Set conditions for printing the header portion of the clerk totals report
*
*
* @rdesc return values.
* @flag int    |  No return value
*
*
* @end
**********************************************************************************************/

void vdSetClerkTotalHeaderCondition(int inReportTop,int inGrandTotals) {
    if (inGetPrinterType() == NOPRINTER || !fGetEnablePrinter())
        return;

    lnHeaderCondition = formater_set_flags((inReportTop && fGetDemoMode() == VS_TRUE), ((inGetTipProcessing() > TIP_OFF) && fGetCashBack()),
        (fGetCashBack()),
        (inGetTipProcessing() > TIP_OFF),

        0,
        (inGetApplicationType() == RETAIL && inGetClerkMode() != NO_CLERK),
        0,
        (inGetApplicationType() == RESTAURANT && inGetClerkMode() != NO_CLERK),

        (inGetApplicationType() == RETAIL && inGetClerkMode() != NO_CLERK && inReportTop),
        0,
        0,
        0,

        (inGetApplicationType() == RESTAURANT && inGetClerkMode() != NO_CLERK && inReportTop),
        0,
        0,
        0,

        0,
        0,
        0,
        (inGetTipProcessing() > TIP_OFF || (fGetCashBack())),  /* added for 460 */

        0,
        (inGetApplicationType() == RETAIL),                 /* added for 460 */
        0,
        (inGetApplicationType() == RESTAURANT),             /* added for 460 */

        (inGetTipProcessing() == TIP_OFF && !(fGetCashBack())), /* added for 460 */
        (inGetTipProcessing() > TIP_OFF && !(fGetCashBack())), /* added for 460 */
        (inGetTipProcessing() == TIP_OFF && fGetCashBack()),    /* added for 460 */
        (inGetTipProcessing() > TIP_OFF && fGetCashBack()),    /* added for 460 */

        0,
        0,
        0,
        0, -1);
}


/*********************************************************************************************
* @func int |vdSetClerkTotalMiddleCondition |
*
*  Set conditions for printing the middle portion of the clerk totals report
*
*
* @rdesc return values.
* @flag int    |  No return value
*
*
* @end
**********************************************************************************************/



void vdSetClerkTotalMiddleCondition(void) {
    lnMiddleCondition = formater_set_flags(0, ((inGetTipProcessing() > TIP_OFF) && fGetCashBack()),
        (fGetCashBack()),
        (inGetTipProcessing() > TIP_OFF),

        0,
        0,
        0,
        0,

        0,
        0,
        0,
        0,

        0,
        0,
        0,
        0,

        0,
        0,
        0,
        (inGetTipProcessing() > TIP_OFF || (fGetCashBack())),  /* added for 460 */

        0,
        0,
        0,
        0,

        0,
        0,
        0,
        0,

        (inGetTipProcessing() > TIP_OFF && (fGetCashBack())),  /* added for 460 */
        (inGetTipProcessing() > TIP_OFF && !(fGetCashBack())), /* added for 460 */
        (inGetTipProcessing() == TIP_OFF && fGetCashBack()),    /* added for 460 */
        (inGetTipProcessing() == TIP_OFF && !(fGetCashBack())), /* added for 460 */ - 1);
}

/*********************************************************************************************
* @func int |vdSetClerkTotalBottomCondition |
*
*  Set conditions for printing the bottom portion of the clerk totals report
*
*
* @rdesc return values.
* @flag int    |  No return value
*
*
* @end
**********************************************************************************************/

void vdSetClerkTotalBottomCondition(TRANSACTION_OBJECT* pobTran,int inFormFeed,long lnSurcharge,long lnTaxTotal) {
    lnBottomCondition = formater_set_flags(inFormFeed, 0, (fGetCashBack()), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, inGetSurCharge(), (inGetTipProcessing() > TIP_OFF),
        (fGetTaxProc() || lnTaxTotal > 0),
        0, -1);
}



/*********************************************************************************************
* @func int |inPrintClerkTotalsRecord |
*
*  Print a clerk totals record
*
*
* @rdesc return values.
* @flag int    |  VS_ERR, VS_SUCCESS
*
*
* @end
**********************************************************************************************/


int inPrintClerkTotalsRecord(PRINTER_OBJECT* obPrinter,CLERK_TOTS_REC* psrClerkTots,int inShift,char* szClerkId) {
    strcpy(srRcpt.unFrm.srClerk.srMid.szClerkId, szClerkId);
    sprintf(srRcpt.unFrm.srClerk.srMid.szTransCnt, SUBSTITUTE_D, psrClerkTots->inCount);
    sprintf(srRcpt.unFrm.srClerk.srMid.szTipAmt, SUBSTITUTE_LD, psrClerkTots->lnTipAmt);
    sprintf(srRcpt.unFrm.srClerk.srMid.szCBackAmt, SUBSTITUTE_LD, psrClerkTots->lnCashBack);
    sprintf(srRcpt.unFrm.srClerk.srMid.szTransAmt, SUBSTITUTE_LD, psrClerkTots->lnTotAmt);
    sprintf(srRcpt.unFrm.srClerk.srMid.szBaseAmt, SUBSTITUTE_LD, psrClerkTots->lnBaseAmt);
    /*
        sprintf(srRcpt.unFrm.srClerk.srMid.szTransCnt,SUBSTITUTE_D, srUTTRec.inNetCount);
        sprintf(srRcpt.unFrm.srClerk.srMid.szTipAmt,SUBSTITUTE_LD, srUTTRec.lnNetTipAmt);
        sprintf(srRcpt.unFrm.srClerk.srMid.szCBackAmt,SUBSTITUTE_LD, srUTTRec.lnNetCashBack);
        sprintf(srRcpt.unFrm.srClerk.srMid.szTransAmt,SUBSTITUTE_LD, srUTTRec.lnNetTotAmt);
        sprintf(srRcpt.unFrm.srClerk.srMid.szBaseAmt,SUBSTITUTE_LD, srUTTRec.lnNetBaseAmt);
    */
    vdFormatAmount(srRcpt.unFrm.srClerk.srMid.szTipAmt, VS_FALSE);
    vdTrimLeadSpaces(srRcpt.unFrm.srClerk.srMid.szTipAmt);
    vdFormatAmount(srRcpt.unFrm.srClerk.srMid.szCBackAmt, VS_FALSE);
    vdTrimLeadSpaces(srRcpt.unFrm.srClerk.srMid.szCBackAmt);
    vdFormatAmount(srRcpt.unFrm.srClerk.srMid.szTransAmt, VS_FALSE);
    vdTrimLeadSpaces(srRcpt.unFrm.srClerk.srMid.szTransAmt);
    vdFormatAmount(srRcpt.unFrm.srClerk.srMid.szBaseAmt, VS_FALSE);
    vdTrimLeadSpaces(srRcpt.unFrm.srClerk.srMid.szBaseAmt);

    vdSetClerkTotalMiddleCondition();
    if (inPrintLines(obPrinter, CLERK_TOTAL_MIDDLE_START, CLERK_TOTAL_MIDDLE_END, NO_BL_LINE, lnMiddleCondition) != VS_SUCCESS)
        return(VS_ERR);

    return(VS_SUCCESS);
}


/*********************************************************************************************
* @func int |inPrintClerkTotalsBottom |
*
*  Initiates printing of clerk totals report bottom section
*
*
*
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ERR
*
*
* @end
**********************************************************************************************/

int inPrintClerkTotalsBottom(TRANSACTION_OBJECT* pobTran,PRINTER_OBJECT* obPrinter,CLERK_TOTS_REC* psrGrandTots,int inFormFeed) {
    sprintf(srRcpt.unFrm.srSubTotal.srTerm.szCreditTotal, SUBSTITUTE_LD, psrGrandTots->lnBaseAmt);
    sprintf(srRcpt.unFrm.srSubTotal.srTerm.szTipTotal, SUBSTITUTE_LD, psrGrandTots->lnTipAmt);
    sprintf(srRcpt.unFrm.srSubTotal.srTerm.szCashBack, SUBSTITUTE_LD, psrGrandTots->lnCashBack);
    sprintf(srRcpt.unFrm.srSubTotal.srTerm.szTotalSchgAmt, SUBSTITUTE_LD, psrGrandTots->lnSurcharge);
    sprintf(srRcpt.unFrm.srSubTotal.srTerm.szNetTotal, SUBSTITUTE_LD, psrGrandTots->lnTotAmt);
    sprintf(srRcpt.unFrm.srSubTotal.srTerm.szTaxTotal, SUBSTITUTE_LD, psrGrandTots->lnTaxAmt);

    vdFormatAmount(srRcpt.unFrm.srSubTotal.srTerm.szCreditTotal, VS_TRUE);
    vdFormatAmount(srRcpt.unFrm.srSubTotal.srTerm.szTipTotal, VS_TRUE);
    vdFormatAmount(srRcpt.unFrm.srSubTotal.srTerm.szCashBack, VS_TRUE);
    vdFormatAmount(srRcpt.unFrm.srSubTotal.srTerm.szTotalSchgAmt, VS_TRUE);
    vdFormatAmount(srRcpt.unFrm.srSubTotal.srTerm.szNetTotal, VS_TRUE);
    vdFormatAmount(srRcpt.unFrm.srSubTotal.srTerm.szTaxTotal, VS_TRUE);

    vdSetClerkTotalBottomCondition(pobTran, inFormFeed, psrGrandTots->lnSurcharge, psrGrandTots->lnTaxAmt);
    if (inPrintLines(obPrinter, CLERK_TOTAL_BOTTOM_START, CLERK_TOTAL_BOTTOM_END, NO_BL_LINE, lnBottomCondition) != VS_SUCCESS)
        return(VS_ERR);
    return(VS_SUCCESS);
}


/*********************************************************************************************
* @func int |inPrintClerkTotalsReport |
*
*  Browes through the batch and prints the total report.

*
*
*
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ERR
*
*
* @end
**********************************************************************************************/


int inPrintClerkTotalsReport(TRANSACTION_OBJECT* pobTran,PRINTER_OBJECT* obPrinter) {
    int inStart,
    inStop,
    inMerch = 0;
    int inHostIndex;
    int inGoodClerk;
    ADT_REC srADTRec;
    FHANDLE hADTFHandle;
    char szADTFileName[FILENAME_SIZE + 1];
    UTT_REC srUTTRec;
    UTT_KEY srUTTKey;
    char szUTTFileName[FILENAME_SIZE + 1] ;
    DB_FILE srDBFPtr;
    int inRetVal;
    int inReportTop = VS_TRUE;
    CLERK_TOTS_REC srGrandTots;
    CLERK_TOTS_REC srClerkTots;

    memset((char *) &srGrandTots, 0x00, (SIZE_CLERK_TOTS_REC));

    if (fGetEnablePrinter())
        vdDisplayMessageFromFile(PRINTING_MSG, STATUS_MSG);

    if (inLoadMITRec(0) != VS_SUCCESS)
        return(VS_ERR);
    inCombineActiveClerks();
    /* This is useing temp ADT file created in CombineActiveClerks  */
    vdGetMessageFromFile(ADT_FILENAME, szADTFileName);

    /* open ADT table */
    if ((hADTFHandle = open(szADTFileName, O_RDWR)) < 0) {
        vdDisplayMessageFromFile(BATCH_EMPTY_MSG, WARNING_MSG);
        SVC_WAIT(1000);
        return(VS_ERR);
    }

    inStart = 1;
    inStop = inGetMITTotRecs();


    inGoodClerk = read(hADTFHandle, (char *) &srADTRec, SIZE_ADT_REC);

    while (inGoodClerk == SIZE_ADT_REC) {
        memset((char *) &srClerkTots, 0x00, SIZE_CLERK_TOTS_REC);
        strcpy(pobTran->srBKey.szClerkId, srADTRec.szClerkId);
        for (inMerch = inStart; inMerch <= inStop; inMerch++) {
            if (inLoadMITRec(inMerch - 1) != VS_SUCCESS)
                return(VS_ERR);

            /* open correct merchant batch */
            if (inGetDCBatchRecCnt(pobTran) == 0)
                continue;

            for (inHostIndex = 0; inHostIndex < MAX_HOSTS; inHostIndex++) {
                //              vdSetHostHandle(inHostIndex);

                if (inLoadDefaultHost(inHostIndex) != VS_SUCCESS)
                    return (VS_ERR);

                if (inLoadDefaultCurrency(pobTran) != VS_SUCCESS)
                    return (VS_ERR);

                /* Only print the report for independent host */
                if (!fIsIndependentBatch(inHostIndex))
                    continue;
                vdGetUTTFileName(szUTTFileName);

                if (inOpenUTT(&srDBFPtr, szUTTFileName) < 0)
                    return(VS_ERR);

                strcpy(srUTTKey.szClerkID, srADTRec.szClerkId);
                inRetVal = inGetKeyUTTRec(&srDBFPtr, &srUTTRec, &srUTTKey, KEY_SVR_ID, DB_FIRST);

                if (inRetVal != VS_ERROR)
                    do {
                        /* Bottom totals */
                        srClerkTots.inCount += srUTTRec.inNetCount;
                        srClerkTots.lnTotAmt += srUTTRec.lnNetTotAmt;
                        srClerkTots.lnBaseAmt += srUTTRec.lnNetBaseAmt;
                        srClerkTots.lnTaxAmt += srUTTRec.lnNetTaxAmt;
                        srClerkTots.lnTipAmt += srUTTRec.lnNetTipAmt;
                        srClerkTots.lnCashBack += srUTTRec.lnNetCashBack;
                        srClerkTots.lnSurcharge += srUTTRec.lnNetSurcharge;
                        srClerkTots.lnTipSale += srUTTRec.lnTipSale;

                        srGrandTots.inCount += srUTTRec.inNetCount;
                        srGrandTots.lnTotAmt += srUTTRec.lnNetTotAmt;
                        srGrandTots.lnBaseAmt += srUTTRec.lnNetBaseAmt;
                        srGrandTots.lnTaxAmt += srUTTRec.lnNetTaxAmt;
                        srGrandTots.lnTipAmt += srUTTRec.lnNetTipAmt;
                        srGrandTots.lnCashBack += srUTTRec.lnNetCashBack;
                        srGrandTots.lnSurcharge += srUTTRec.lnNetSurcharge;
                        srGrandTots.lnTipSale += srUTTRec.lnTipSale;

                        strcpy(srUTTKey.szClerkID, srADTRec.szClerkId);
                        inRetVal = inGetKeyUTTRec(&srDBFPtr, &srUTTRec, &srUTTKey, KEY_SVR_ID, DB_NEXT);
                    } while (inRetVal != VS_ERR);  /* end if */
                inCloseUTT(&srDBFPtr);
            } /* end of host loop */
        } /* end of Merch loop */
        if (inReportTop == VS_TRUE) {
            vdMakeReportHeader(pobTran);
            if (inGetMITTotRecs() == 1)
                inPrintReptHeader(pobTran, obPrinter);
            else {
                if (inPrintLines(obPrinter, REPT_HEADER_START, REPT_HEADER_END, NO_BL_LINE, lnHeaderCondition) != VS_SUCCESS)
                    return(VS_ERR);
            }
            vdSetClerkTotalHeaderCondition(inReportTop, VS_FALSE);
            inReportTop = VS_FALSE;
            if (inPrintLines(obPrinter, CLERK_TOTAL_TOP_START, CLERK_TOTAL_TOP_END, NO_BL_LINE, lnHeaderCondition) != VS_SUCCESS)
                return(VS_ERR);
        }
        inPrintClerkTotalsRecord(obPrinter, &srClerkTots, 0, srADTRec.szClerkId);
        inGoodClerk = read(hADTFHandle, (char *) &srADTRec, SIZE_ADT_REC);
    } /* end clerk loop */

    close(hADTFHandle); /* through with ADT temp file*/
    remove(ADT_FILE_NAME);

    /* Check whether the batch is empty or not */
    if (inReportTop == VS_TRUE) {
        vdDisplayMessageFromFile(BATCH_EMPTY_MSG, WARNING_MSG);
        SVC_WAIT(1000);
        return(VS_ERR);
    } else {
        inPrintClerkTotalsBottom(pobTran, obPrinter, &srGrandTots, VS_TRUE);

        if (inPrintLines(obPrinter, CLERK_SUMMARY_FORM_FEED, CLERK_SUMMARY_FORM_FEED, NO_BL_LINE, 0x80000000) != VS_SUCCESS)
            return(VS_ERR);
    }

    return(VS_SUCCESS);
}


/*********************************************************************************************
* @func int |vdSetClerkTableHeaderCondition |
*
*  Set conditions to print the3 header of the clerk table report
*
*
*
* @rdesc return values.
* @flag int    |  No return value
*
*
* @end
**********************************************************************************************/



void vdSetClerkTableHeaderCondition(void) {
    if (inGetPrinterType() == NOPRINTER || !fGetEnablePrinter())
        return;

    lnHeaderCondition = formater_set_flags(0, (fGetDemoMode() == VS_TRUE), 0, 0, (inGetApplicationType() == RETAIL && inGetClerkMode() == CLERK_LGN), (inGetApplicationType() == RESTAURANT && inGetClerkMode() == CLERK_LGN), 0, 0, (inGetApplicationType() == RETAIL && inGetClerkMode() != CLERK_LGN), (inGetApplicationType() == RESTAURANT && inGetClerkMode() != CLERK_LGN), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1);
}


/*********************************************************************************************
* @func int |vdSetClerkTableMiddleCondition |
*
*  Set conditions for printing the middle section of the clerk table report
*
*
*
* @rdesc return values.
* @flag int    |  no return value
*
*
* @end
**********************************************************************************************/


void vdSetClerkTableMiddleCondition(void) {
    lnMiddleCondition = formater_set_flags(0, 0, 0, 0, (inGetClerkMode() == CLERK_LGN), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1);
}

static char ifOddBoundary(unsigned char* buff) {
    return (char) (((long) buff) % 2);
}


/*********************************************************************************************
* @func int |inPrintClerkTableReport |
*
*  Initiates printing of the clerk table report
*
*
*
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ERR
*
*
* @end
**********************************************************************************************/


int inPrintClerkTableReport(PRINTER_OBJECT* obPrinter,TRANSACTION_OBJECT* pobTran) {
    unsigned char ucUDTRecBuff[SIZE_UDT_REC + 1];
    unsigned char * ucUDTRec = ucUDTRecBuff;
    FHANDLE hUDTFHandle;
    int inRecordSize;

    if (ifOddBoundary(ucUDTRec))   /* if on odd boundary increment pointer */
        ucUDTRec++;     
    if (fGetEnablePrinter())
        vdDisplayMessageFromFile(PRINTING_MSG, STATUS_MSG);


    vdMakeReportHeader(pobTran);
    if (inGetMITTotRecs() == 1) {
        /* If there is only one merchant then print its header info - bug MM2 */
        if (inLoadMITRec(0) != VS_SUCCESS)
            return(VS_ERR);

        inPrintReceiptHeader(pobTran);
    }

    vdSetClerkTableHeaderCondition();

    if (inPrintLines(obPrinter, CLERK_TABLE_TOP_START, CLERK_TABLE_TOP_END, NO_BL_LINE, lnHeaderCondition) != VS_SUCCESS)
        return(VS_ERR);

    if ((hUDTFHandle = open(UDT_FILENAME, O_RDWR)) < 0)
        return(VS_ERR);

    if (lseek(hUDTFHandle, (long) GEN_VER_SIZE, SEEK_SET) != (long) GEN_VER_SIZE) {
        close(hUDTFHandle);
        return(VS_ERR);
    }

    vdSetClerkTableMiddleCondition();
    do {
        if ((inRecordSize = read(hUDTFHandle, (char *) ucUDTRec, SIZE_UDT_REC)) == SIZE_UDT_REC) {
            strcpy(srRcpt.szClerkId, szGetClerkIdFromStruct(ucUDTRec));
            strcpy(srRcpt.szInvNum, szGetClerkPasswdFromStruct(ucUDTRec));

            if (inGetClerkMode() == CLERK_LGN) {
                if (inGetLogedInFromStruct(ucUDTRec))
                    strcpy(srRcpt.unFrm.srReceipt.szCType, "Yes");
                else
                    strcpy(srRcpt.unFrm.srReceipt.szCType, "No");
            }
            if (inPrintLines(obPrinter, CLERK_TABLE_MIDDLE_START, CLERK_TABLE_MIDDLE_END, NO_BL_LINE, lnMiddleCondition) != VS_SUCCESS) {
                close(hUDTFHandle);
                return(VS_ERR);
            }
            if (act_kbd_pending_test(KEY_CANCEL)) {
                close(hUDTFHandle);
                return(VS_ESCAPE);
            }
        }
    } while (inRecordSize == SIZE_UDT_REC);

    close(hUDTFHandle);

    if (inPrintLines(obPrinter, CLERK_TABLE_BOTTOM_START, CLERK_TABLE_BOTTOM_END, NO_BL_LINE, 0L) != VS_SUCCESS)
        return(VS_ERR);

    return(VS_SUCCESS);
}





/*********************************************************************************************
* @func int |vdSetIRSTipHeaderCondition |
*
*  Set conditions for printing the header portion of the IRS Tip Report
*
*
*
* @rdesc return values.
* @flag int    |  No return value
*
*
* @end
**********************************************************************************************/


void vdSetIRSTipHeaderCondition(VS_BOOL fTotals) {
    if (inGetPrinterType() == NOPRINTER || !fGetEnablePrinter())
        return;

    lnHeaderCondition = formater_set_flags(0, (fGetDemoMode() == VS_TRUE), 0, fGetCashTipOn(), ((inGetApplicationType() == RESTAURANT) && (fTotals == VS_FALSE)), ((inGetApplicationType() == RETAIL) && (fTotals == VS_FALSE)), (fTotals == VS_TRUE), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1);
}



/*********************************************************************************************
* @func int |vdSetIRSTipBottomCondition |
*
*  Set conditions for printing the bottom portion of the IRS Tip Report
*
*
*
* @rdesc return values.
* @flag int    |  No return value
*
*
* @end
**********************************************************************************************/


void vdSetIRSTipBottomCondition(void) {
    lnBottomCondition = formater_set_flags(((fGetClerkSignLine() == VS_TRUE) && (inGetApplicationType() == RESTAURANT)), ((fGetClerkSignLine() == VS_TRUE) && (inGetApplicationType() == RETAIL)), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1);
}


/*********************************************************************************************
* @func int |vdSetIRSNoTransCondition |
*
*  Set conditions for printing the IRS Tip Report when there are no transactions
*
*
*
* @rdesc return values.
* @flag int    |  No return value
*
*
* @end
**********************************************************************************************/


void vdSetIRSNoTransCondition(void) {
    if (inGetPrinterType() == NOPRINTER || !fGetEnablePrinter())
        return;
    lnBottomCondition = formater_set_flags(0, 0, 0, 0, (inGetApplicationType() == RESTAURANT), (inGetApplicationType() == RETAIL), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1);
}

/*********************************************************************************************
* @func int |inCombineActiveClerks |
*
*  Read all host ADT files and combines them into one temporary
*  file which will hold all of the clerks with activity in the
*  current batch regardless of the host they accessed.
*
*
*
*
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ERR
*
*
* @end
**********************************************************************************************/

/* #ifdef BATCH_DEFINED */


int inCombineActiveClerks(void) {
    int inHostIndex = 0;
    int inRecordSize;
    char szADTFileName[FILENAME_SIZE + 1];
    char szTmpADTFileName[FILENAME_SIZE + 1];
    int inMerch,
    inStop;
    int inMerchNum = inGetMerchantNum();

    ADT_REC srADTRec;
    FHANDLE hADTFHandle;

    /** /pdebug(("--inCombineActiveClerks--")); */

    /* New file for temporary ADT storage */
    vdGetMessageFromFile(ADT_FILENAME, szTmpADTFileName);
    inRemoveFile(szTmpADTFileName);

    inStop = inGetMITTotRecs();

    for (inMerch = 1; inMerch <= inStop; inMerch++) {
        if (inLoadMITRec(inMerch - 1) != VS_SUCCESS)
            return(VS_ERR);
        for (inHostIndex = 0; inHostIndex < MAX_HOSTS; inHostIndex++) {
            sprintf(szADTFileName, "ADT%02d%02d.DAT", inHostIndex, inGetMerchantNum());
            if ((hADTFHandle = open(szADTFileName, O_RDWR)) < 0)
                continue;

            do {
                if ((inRecordSize = read(hADTFHandle, (char *) &srADTRec, SIZE_ADT_REC)) == SIZE_ADT_REC) {
                    if (inInsertADTEntry(srADTRec.szClerkId, szTmpADTFileName) != VS_SUCCESS) {
                        /* Reload transaction merchant */
                        if (inLoadMITRec(inMerchNum - 1) != VS_SUCCESS) {
                            close(hADTFHandle);
                            return(VS_ERR);
                        }
                        vdDisplayMessageFromFile(ADT_FILE_ERR_MSG21, ERROR_MSG);
                        close(hADTFHandle);
                        return(VS_ERR);
                    }
                }
            } while (inRecordSize == SIZE_ADT_REC);

            close(hADTFHandle);
        } /* end for loop */
    } /* end of merch loop */
    /* Reload transaction merchant */
    if (inLoadMITRec(inMerchNum - 1) != VS_SUCCESS)
        return(VS_ERR);
    return(VS_SUCCESS);
} /* end of inCombineActiveClerks() */


/*********************************************************************************************
* @func int |inPrintIRSTipOneClerk |
*
*  Initiates printing of the IRS tip report when the one clerk option is chosen
*
*
*
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ERR
*
*
* @end
**********************************************************************************************/


int inPrintIRSTipOneClerk(PRINTER_OBJECT* obPrinter,char* szClerkId,long lnTipSale,long lnTipAmount,long lnCashTipAmt) {
    long lnTotalTipAmt;
    double dbTipPercent;

    lnTotalTipAmt = lnCashTipAmt + lnTipAmount;

    sprintf(srRcpt.unFrm.srDet.srMid.szTotalSaleAmt, SUBSTITUTE_LD, lnTipSale);
    sprintf(srRcpt.unFrm.srDet.srMid.szTotalTipAmt, SUBSTITUTE_LD, lnTipAmount);
    sprintf(srRcpt.unFrm.srDet.srMid.szGrandTotal, SUBSTITUTE_LD, lnTipSale);
    sprintf(srRcpt.unFrm.srDet.srMid.szCashTipAmt, SUBSTITUTE_LD, lnCashTipAmt);
    sprintf(srRcpt.unFrm.srDet.srMid.szAllTipAmt, SUBSTITUTE_LD, lnTotalTipAmt);


    if ((lnTipSale > 0) && (lnTipAmount > 0))  /* bb1 */
        dbTipPercent = ((double) lnTipAmount) / ((double) (lnTipSale));
    else
        dbTipPercent = 0.0;

    /* If our percentage is going to be > 99.9 % then just print that so */
    /* we don't over flow - bug # Gina_B2 130 */
    if (dbTipPercent > (double) 1L)
        dbTipPercent = 0.999;

    //    sprintf(srRcpt.unFrm.srDet.srMid.szTipPercent, "%3.1f", dbTipPercent*100.00);
    sprintf(srRcpt.unFrm.srDet.srMid.szTipPercent, "%4.2f", dbTipPercent * 100.00);
    vdFormatAmount(srRcpt.unFrm.srDet.srMid.szGrandTotal, VS_TRUE);
    vdFormatAmount(srRcpt.unFrm.srDet.srMid.szTotalSaleAmt, VS_TRUE);
    vdFormatAmount(srRcpt.unFrm.srDet.srMid.szTotalTipAmt, VS_TRUE);
    vdFormatAmount(srRcpt.unFrm.srDet.srMid.szCashTipAmt, VS_TRUE);
    vdFormatAmount(srRcpt.unFrm.srDet.srMid.szAllTipAmt, VS_TRUE);

    strcpy(srRcpt.unFrm.srDet.srMid.szClerkId, szClerkId);
    lnMiddleCondition = lnHeaderCondition;

    if (inPrintLines(obPrinter, IRS_TIP_MIDDLE_START, IRS_TIP_MIDDLE_END, NO_BL_LINE, lnMiddleCondition) != VS_SUCCESS)
        return(VS_ERR);

    return (VS_SUCCESS);
}


/*********************************************************************************************
* @func int |inPrintIRSGrandTotals |
*
*  Initiates printing of grand totals section of the IRS tip report
*
*
*
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ERR
*
*
* @end
**********************************************************************************************/

int inPrintIRSGrandTotals(PRINTER_OBJECT* obPrinter,long lnTipSale,long lnTipAmount,long lnCashTipAmt) {
    long lnTotalTipAmt;
    double dbTipPercent;

    lnTotalTipAmt = lnCashTipAmt + lnTipAmount;

    sprintf(srRcpt.unFrm.srDet.srMid.szTotalSaleAmt, SUBSTITUTE_LD, lnTipSale);
    sprintf(srRcpt.unFrm.srDet.srMid.szTotalTipAmt, SUBSTITUTE_LD, lnTipAmount);
    sprintf(srRcpt.unFrm.srDet.srMid.szGrandTotal, SUBSTITUTE_LD, lnTipSale);
    sprintf(srRcpt.unFrm.srDet.srMid.szCashTipAmt, SUBSTITUTE_LD, lnCashTipAmt);
    sprintf(srRcpt.unFrm.srDet.srMid.szAllTipAmt, SUBSTITUTE_LD, lnTotalTipAmt);


    if ((lnTipSale > 0) && (lnTipAmount > 0))  /* bb1 */
        dbTipPercent = ((double) lnTipAmount) / ((double) (lnTipSale));
    else
        dbTipPercent = 0.0;

    /* If our percentage is going to be > 99.9 % then just print that so */
    /* we don't over flow - bug # Gina_B2 130 */
    if (dbTipPercent > (double) 1L)
        dbTipPercent = 0.999;

    //    sprintf(srRcpt.unFrm.srDet.srMid.szTipPercent, "%3.1f", dbTipPercent*100.00);
    sprintf(srRcpt.unFrm.srDet.srMid.szTipPercent, "%4.2f", dbTipPercent * 100.00);
    vdFormatAmount(srRcpt.unFrm.srDet.srMid.szGrandTotal, VS_TRUE);
    vdFormatAmount(srRcpt.unFrm.srDet.srMid.szTotalSaleAmt, VS_TRUE);
    vdFormatAmount(srRcpt.unFrm.srDet.srMid.szTotalTipAmt, VS_TRUE);
    vdFormatAmount(srRcpt.unFrm.srDet.srMid.szCashTipAmt, VS_TRUE);
    vdFormatAmount(srRcpt.unFrm.srDet.srMid.szAllTipAmt, VS_TRUE);

    vdSetIRSTipHeaderCondition(VS_TRUE);
    lnMiddleCondition = lnHeaderCondition;

    if (inPrintLines(obPrinter, IRS_TIP_MIDDLE_START, IRS_TIP_MIDDLE_END, NO_BL_LINE, lnMiddleCondition) != VS_SUCCESS)
        return(VS_ERR);

    return (VS_SUCCESS);
}



/*********************************************************************************************
* @func int |vdSetClerkHeaderCondition |
*
*  Set conditions for printing the header portion of the clerk detail report
*
*
*
* @rdesc return values.
* @flag int    |  No return value
*
*
* @end
**********************************************************************************************/


void vdSetClerkHeaderCondition(int inReportTop,int inReportType,int inNewClerk,int inCurrencyFlag) {
    if (inGetPrinterType() == NOPRINTER || !fGetEnablePrinter())
        return;

    lnHeaderCondition = formater_set_flags((inReportTop && inGetApplicationType() == RETAIL), (inReportTop && fGetDemoMode() == VS_TRUE), (inReportTop && inGetApplicationType() == RESTAURANT), 0, (inGetApplicationType() == RESTAURANT && inNewClerk), (inGetApplicationType() == RETAIL && inNewClerk), 0, 0, (inGetMITTotRecs() > 1),
        0,
        0,
        0,

        0,
        (inGetCSTTotRecs() > 1 && inCurrencyFlag == VS_TRUE),
        0,
        0,

        (inGetTipProcessing() > TIP_OFF),
        (fGetCashBack()),
        0,
        0,

        (inGetTipProcessing() > TIP_OFF && fGetCashBack()),
        (inGetTipProcessing() > TIP_OFF && !(fGetCashBack())),
        (inGetTipProcessing() == TIP_OFF && fGetCashBack()),
        (inGetTipProcessing() == TIP_OFF && !(fGetCashBack())),

        0,
        0,
        0,
        0,

        0,
        (inReportType), /* && (inGetShiftType() == NO_SHIFT)), bb1 */
        (inReportType), /* && (inGetShiftType() != NO_SHIFT)), bb1 */
        (!inReportType), -1);
}



/*********************************************************************************************
* @func int |vdSetClerkMiddleCondition |
*
*  Set conditions for printing the middle portion of the clerk detail report
*
*
*
* @rdesc return values.
* @flag int    |  No return value
*
*
* @end
**********************************************************************************************/


void vdSetClerkMiddleCondition(TRANSACTION_OBJECT* pobTran) {
    lnMiddleCondition = formater_set_flags(((fRefundTrans(pobTran) || pobTran->srBKey.fVoided) && (inGetTipProcessing() > TIP_OFF) && (fGetCashBack())),
        ((fRefundTrans(pobTran) || pobTran->srBKey.fVoided) && (inGetTipProcessing() > TIP_OFF) && !(fGetCashBack())),
        ((fRefundTrans(pobTran) || pobTran->srBKey.fVoided) && (inGetTipProcessing() == TIP_OFF) && (fGetCashBack())),
        ((fRefundTrans(pobTran) || pobTran->srBKey.fVoided) && (inGetTipProcessing() == TIP_OFF) && !(fGetCashBack())),

        (!(fRefundTrans(pobTran) || pobTran->srBKey.fVoided) && (inGetTipProcessing() > TIP_OFF) && (fGetCashBack())),
        (!(fRefundTrans(pobTran) || pobTran->srBKey.fVoided) && (inGetTipProcessing() > TIP_OFF) && !(fGetCashBack())),
        (!(fRefundTrans(pobTran) || pobTran->srBKey.fVoided) && (inGetTipProcessing() == TIP_OFF) && (fGetCashBack())),
        (!(fRefundTrans(pobTran) || pobTran->srBKey.fVoided) && (inGetTipProcessing() == TIP_OFF) && !(fGetCashBack())),

        (fRefundTrans(pobTran) || pobTran->srBKey.fVoided),
        0,
        0,
        0,

        0,
        0,
        0,
        0,

        0,
        0,
        0,
        (inGetTipProcessing() > TIP_OFF || (fGetCashBack())),  /* added for 460 */

        0,
        0,
        0,
        0,

        0,
        0,
        0,
        0,

        0,
        0,
        0,
        0, -1);
}


/*********************************************************************************************
* @func int |inGetClerkSelection |
*
*  Select one or all clerks
*
* @parm char * | pszClerkId |
*       Buffer to hold clerk Id.
*
*
* @rdesc return values.
* @flag int    |  VS_ESCAPE
* @flag int    |  KEY_ONERPT
* @flag int    |  KEY_ALLRPT
*
*
* @end
**********************************************************************************************/
int inGetClerkSelection(char* pszClerkId) {
    signed int inPrintAll,
    inResult;
    long lnMsgOffset,
    lnMsgId;
    char* chNull = 0;

    if (inGetApplicationType() == RESTAURANT)
        lnMsgOffset = ALLONE_REST_MSG15;
    else
        lnMsgOffset = ALLONE_MSG15;

    inPrintAll = obDisp.inDO_ChooseOne(chNull, (char *) pchGetMsg(lnMsgOffset), (char *) pchGetMsg(CLERKDET_OPTION_MSG5x), 2, NULL_PFI);

    if (inPrintAll == VS_ESCAPE)
        return(VS_ESCAPE);
    if (inPrintAll == KEY_ONERPT)    /* print one clerk option */ {
        do {
            vdDispMsgAt(EMPTY_STR_ERR_MSG, obDisp.inHomeCol, obDisp.inDetailLine, CLR_EOL);
            vdDispMsgAt(EMPTY_STR_ERR_MSG, obDisp.inHomeCol, obDisp.inInputLine, CLR_EOL);
            vdDisplayMessageFromFile(EMPTY_STR_ERR_MSG, STATUS_MSG);
            pszClerkId[0] = '\0';

            if (inGetApplicationType() == RETAIL)
                lnMsgId = CLERK_ID_MSG;
            else
                lnMsgId = SERVER_ID_MSG;

            inResult = inEnterWithMessage(lnMsgId, SCROLL_DISPLAY_TRUE, pszClerkId, INPUT_ALNUMS, CLERK_ID_SIZE, NULL_PFI);
            if (inResult == VS_ESCAPE)
                return(VS_ESCAPE);

            if (inValidateClerk(pszClerkId) != VS_SUCCESS)
                vdDisplayMessageFromFile(lnGGErrorMessage(), ERROR_MSG);
            else
                break;
        } while (1);

        if (inResult == VS_ESCAPE)
            return(VS_ESCAPE);
    }  /* end of inPrintAll == KEY_ONERPT */
    return (inPrintAll);
}

/*********************************************************************************************
* @func int |inPrintSingleClerkTipDisc  |
*
*  Initiates printing of the discount tip report when the one clerk option is chosen
*
*
*
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ERR
*
*
* @end
**********************************************************************************************/


int inPrintSingleClerkTipDisc(PRINTER_OBJECT* obPrinter,TRANSACTION_OBJECT* pobTran,DISCTIP_TOTALS* psrTot) {
    vdMakeDiscTipMiddle(pobTran, psrTot);
    vdSetClerkMiddleCondition(pobTran);
    if (inPrintLines(obPrinter, DISCNT_TIP_MIDDLE_START, DISCNT_TIP_MIDDLE_END, NO_BL_LINE, lnMiddleCondition) != VS_SUCCESS)
        return(VS_ERR);
    return (VS_SUCCESS);
}


/*********************************************************************************************
* @func int |vdSetClerkTipHeaderCondition  |
*
*  Set conditions for printing the header portion of the discount tip report
*
*
*
* @rdesc return values.
* @flag int    |  No return value
*
*
* @end
**********************************************************************************************/


void vdSetClerkTipHeaderCondition(int inReportTop,int inReportType,int inNewClerk) {
    if (inGetPrinterType() == NOPRINTER || !fGetEnablePrinter())
        return;

    lnHeaderCondition = formater_set_flags(0, (inReportTop && fGetDemoMode() == VS_TRUE), 0, 0, (inGetApplicationType() == RESTAURANT && inNewClerk), (inGetApplicationType() == RETAIL && inNewClerk), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1);
}

/*********************************************************************************************
* @func int |inPrintDiscountTipReport |
*
*  Initiates printing of the discount tip report
*
*
*
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ERR
*
*
* @end
**********************************************************************************************/



/* #ifdef BATCH_DEFINED */


int inPrintDiscountTipReport(TRANSACTION_OBJECT* pobTran,PRINTER_OBJECT* obPrinter) {
#ifdef BATCH_DEFINED


    ADT_REC srADTRec;
    FHANDLE hADTFHandle;
    char szADTFileName[FILENAME_SIZE + 1];
    int inRecordSize;

    int inResult,
    inNeedGrTot,
    inNeedSub;
    char szClerkId[CLERK_ID_SIZE + 1];

    int inReportTop = VS_TRUE;

    int inPrintAll = KEY_ALLRPT;
    int inNewClerk = VS_FALSE;
    int inStart,
    inStop,
    inNumberOfMerchants,
    inMerch = 0;

    long lnCondition = 0L;

    DISCTIP_TOTALS srTot;
    DISCTIP_TOTALS srGrTot;

    int inTransPrinted = 0;

    /** /pdebug(("--inPrintDiscountTipReport--"));*/
    inNumberOfMerchants = inSelectOneAllMID(pobTran, fGetMultiMerchPrompt());
    if (inNumberOfMerchants == 0)
        return (VS_SUCCESS);

    if (inNumberOfMerchants == inGetMITTotRecs()) {
        inStart = 1;
        inStop = inGetMITTotRecs();
    } else if (inNumberOfMerchants == 1) {
        inStart = inGetMerchantNum();
        inStop = inGetMerchantNum();
    } else {
        inStart = 1;
        inStop = 0;
    }
    inCombineActiveClerks();
    /* This is useing temp ADT file created in CombineActiveClerks  */
    vdGetMessageFromFile(ADT_FILENAME, szADTFileName);

    /* open ADT table */
    if ((hADTFHandle = open(szADTFileName, O_RDWR)) < 0) {
        vdDisplayMessageFromFile(BATCH_EMPTY_MSG, WARNING_MSG);
        SVC_WAIT(1000);
        return(VS_ERR);
    }


    for (inMerch = inStart; inMerch <= inStop; inMerch++) {
        lseek(hADTFHandle, 0L, SEEK_SET);

        if (inLoadMITRec(inMerch - 1) != VS_SUCCESS) {
            close(hADTFHandle);
            return(VS_ERR);
        }

        /* open correct merchant batch */
        if (inGetDCBatchRecCnt(pobTran) == 0)
            continue;

        /* Need this for inGetDCKeyBatchRec() below */
        vdBatchDCOpen(pobTran);

        inNeedGrTot = 0;

        srGrTot.lnGrandAmt = srGrTot.lnTipAmt = srGrTot.lnNetTipTot = srGrTot.lnTipTot = 0L;
        inTransPrinted = 0;
        if (obDisp.inTitleLine == obDisp.inStatusLine || obDisp.inTitleLine == obDisp.inInputLine)
            clrscr();

        if (fGetEnablePrinter())
            vdDisplayMessageFromFile(PRINTING_MSG, STATUS_MSG);

        /*print the Header lines*/
        vdMakeReportHeader(pobTran);
        inPrintReptHeader(pobTran, obPrinter);
        vdSetClerkTipHeaderCondition(inReportTop, CLERK_REPORT, inNewClerk);

        if (inPrintLines(obPrinter, DISCNT_RPT_HEADER_START, DISCNT_RPT_HEADER_END, NO_BL_LINE, lnHeaderCondition) != VS_SUCCESS) {
            close(hADTFHandle);
            return(VS_ERR);
        }
        do {
            if ((inRecordSize = read(hADTFHandle, (char *) &srADTRec, SIZE_ADT_REC)) == SIZE_ADT_REC) {
                strcpy(pobTran->srBKey.szClerkId, srADTRec.szClerkId);
                strcpy(srRcpt.unFrm.srDet.srMid.szClerkId, srADTRec.szClerkId);
                inNewClerk = VS_TRUE;
                inNeedSub = 0;
                /*  reset sub total for each clerk  */
                srTot.lnGrandAmt = srTot.lnTipAmt = srTot.lnNetTipTot = srTot.lnTipTot = 0L;

                if ((inPrintAll == KEY_ALLRPT) /*|| ( (inPrintAll == KEY_ONERPT) && (strcmp(szClerkId,srUDTRec.szClerkId) == 0))*/) {
                    /*  If record does not exist then go to next clerk  */

                    /* Print online records */
                    inResult = inGetDCKeyBatchRec(pobTran, KEY_SVR_ID, &pobTran->srBKey, DB_FIRST);

                    if ((inResult != VS_ERROR) && (inResult != VS_EOB))
                        do {
                            if ((inGGCode() != AUTH_ONLY && inGGCode() != IMAM_CARDVER) && (!pobTran->srBKey.fVoided)) {
                                if (inNewClerk) {
                                    /* Header condition should not print report title except at top of report */
                                    vdSetClerkTipHeaderCondition(inReportTop, CLERK_REPORT, inNewClerk);
                                    inNewClerk = VS_FALSE;
                                    inReportTop = VS_FALSE;

                                    if (inGetClerkMode() == NO_CLERK)
                                        strcpy(srRcpt.unFrm.srDet.srMid.szClerkId, " ");

                                    if (inPrintLines(obPrinter, DISCNT_TIP_HEADER_START, DISCNT_TIP_HEADER_END, NO_BL_LINE, lnHeaderCondition) != VS_SUCCESS) {
                                        close(hADTFHandle);
                                        return(VS_ERR);
                                    }
                                }

                                if (inPrintSingleClerkTipDisc(obPrinter, pobTran, &srTot) != VS_SUCCESS) {
                                    close(hADTFHandle);
                                    return(VS_ERR);
                                }

                                if (act_kbd_pending_test(KEY_CANCEL)) {
                                    close(hADTFHandle);
                                    return(VS_ESCAPE);
                                }

                                inTransPrinted++;
                            }
                            inResult = inGetDCKeyBatchRec(pobTran, KEY_SVR_ID, &pobTran->srBKey, DB_NEXT);
                            if (inResult == VS_SUCCESS)
                                inNeedSub++;
                        } while ((inResult != VS_ERROR) && (inResult != VS_EOB));
                        /* end of key */
                    if (inNeedSub > 0 && inTransPrinted > 0) {
                        sprintf(srRcpt.unFrm.srDet.srMid.szGrandTotal, SUBSTITUTE_LD, srTot.lnGrandAmt);
                        sprintf(srRcpt.unFrm.srDet.srMid.szAllTipAmt, SUBSTITUTE_LD, srTot.lnNetTipTot);
                        sprintf(srRcpt.unFrm.srDet.srMid.szTotalTipAmt, SUBSTITUTE_LD, srTot.lnTipAmt);
                        sprintf(srRcpt.unFrm.srDet.srMid.szAmt, SUBSTITUTE_LD, srTot.lnTipTot);

                        vdFormatAmount(srRcpt.unFrm.srDet.srMid.szGrandTotal, VS_TRUE);
                        vdFormatAmount(srRcpt.unFrm.srDet.srMid.szAllTipAmt, VS_TRUE);
                        vdFormatAmount(srRcpt.unFrm.srDet.srMid.szTotalTipAmt, VS_TRUE);
                        vdFormatAmount(srRcpt.unFrm.srDet.srMid.szAmt, VS_TRUE);

                        if (inPrintLines(obPrinter, DISCNT_TIP_SUBTOT_START, DISCNT_TIP_SUBTOT_END, NO_BL_LINE, lnHeaderCondition) != VS_SUCCESS) {
                            close(hADTFHandle);
                            return(VS_ERR);
                        }
                    }
                    srGrTot.lnGrandAmt += srTot.lnGrandAmt;
                    srGrTot.lnTipAmt += srTot.lnTipAmt;
                    srGrTot.lnNetTipTot += srTot.lnNetTipTot;
                    srGrTot.lnTipTot += srTot.lnTipTot;

                    if (inPrintAll == KEY_ALLRPT)
                        inNeedGrTot++;
                    strcpy(srRcpt.szErrorMessage, " ");
                    strcpy(srRcpt.szStatusMessage, " ");
                }/* end of print all clerk or Clerk ID entered match the one read from UDT */
            }    /* end of for loop - clerks*/
        } while (inRecordSize == (int) SIZE_ADT_REC);

        if (inNeedGrTot > 1 && inTransPrinted > 0) {
            /* Grand Totals */

            sprintf(srRcpt.unFrm.srDet.srMid.szGrandTotal, SUBSTITUTE_LD, srGrTot.lnGrandAmt);
            sprintf(srRcpt.unFrm.srDet.srMid.szTotalTipAmt, SUBSTITUTE_LD, srGrTot.lnTipAmt);
            sprintf(srRcpt.unFrm.srDet.srMid.szAllTipAmt, SUBSTITUTE_LD, srGrTot.lnNetTipTot);
            sprintf(srRcpt.unFrm.srDet.srMid.szAmt, SUBSTITUTE_LD, srGrTot.lnTipTot);

            vdFormatAmount(srRcpt.unFrm.srDet.srMid.szGrandTotal, VS_TRUE);
            vdFormatAmount(srRcpt.unFrm.srDet.srMid.szTotalTipAmt, VS_TRUE);
            vdFormatAmount(srRcpt.unFrm.srDet.srMid.szAllTipAmt, VS_TRUE);
            vdFormatAmount(srRcpt.unFrm.srDet.srMid.szAmt, VS_TRUE);

            if (inPrintLines(obPrinter, DISCNT_TIP_GRANDTOT_START, DISCNT_TIP_GRANDTOT_END, NO_BL_LINE, lnCondition) != VS_SUCCESS) {
                close(hADTFHandle);
                return(VS_ERR);
            }
        }


        vdCloseDCBatch(pobTran);
        /*  if no transaction printed */
        if (inTransPrinted == 0) {
            if (inGetClerkMode() == NO_CLERK)
                strcpy(srRcpt.unFrm.srDet.srMid.szClerkId, " ");
            else
                strcpy(srRcpt.unFrm.srDet.srMid.szClerkId, szClerkId);
            vdSetClerkTipHeaderCondition(inReportTop, CLERK_REPORT, VS_TRUE);
            if (inPrintLines(obPrinter, DISCNT_TIP_HEADER_START, DISCNT_TIP_HEADER_START, NO_BL_LINE, lnHeaderCondition) != VS_SUCCESS) {
                close(hADTFHandle);
                return(VS_ERR);
            }                

            if (inPrintLines(obPrinter, DISCNT_TIP_GRANDTOT_END, DISCNT_TIP_GRANDTOT_END, NO_BL_LINE, 0x40000000) != VS_SUCCESS) {
                close(hADTFHandle);
                return(VS_ERR);
            }
        }

        if (inPrintLines(obPrinter, DISCNT_TIP_FORMFEED_START, DISCNT_TIP_FORMFEED_END, NO_BL_LINE, 0x80000000) != VS_SUCCESS) {
            close(hADTFHandle);
            return(VS_ERR);
        }
    }
    close(hADTFHandle);
    if (inReportTop == VS_TRUE) {
        vdDisplayMessageFromFile(BATCH_EMPTY_MSG, WARNING_MSG);
        return(VS_ERR);
    }
    return (VS_SUCCESS);
#endif /* batch defined */
}



/*********************************************************************************************
* @func int |inDiscountTipReportOperation |
*
*  Initiates the discount tip report printing operation
*
*
*
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ERR
*
*
* @end
**********************************************************************************************/


int inDiscountTipReportOperation(TRANSACTION_OBJECT* pobTran) {
    /** /pdebug(("--inDiscountTipReportOperation--"));*/

    if (inGetPrinterType() == NOPRINTER && fGetEnablePrinter()) {
        vdDisplayMessageFromFile(NO_PRINTER_MSG, WARNING_MSG);
        return(VS_SUCCESS);
    }

    if (!fGetEnablePrinter())
        return(VS_SUCCESS);

    if (inGetClerkMode() == NO_CLERK || inGetTipProcessing() == TIP_OFF) {
        vdDisplayMessageFromFile(TRANS_NOT_ALLWD_ERR_MSG, ERROR_MSG);
        return(VS_ERR);
    }

    if (inGetPrinterType() != NOPRINTER) {
        inGetClerkTemplate();

        inPrintDiscountTipReport(pobTran, &obPrinter);
    }
    return(VS_SUCCESS);
}


/*********************************************************************************************
* @func int |inIRSTipReportOperation  |
*
*  Initiates the IRS Tip Report printing operation
*
*
*
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ERR
*
*
* @end
**********************************************************************************************/



int inIRSTipReportOperation(TRANSACTION_OBJECT* pobTran) {
    /*    pdebug(("--inIRSTipReportOperation--"));*/
    /* KV1 970407 - IRS Report w/ CASH Tip OFF */
    if (inGetClerkMode() == NO_CLERK || inGetTipProcessing() == TIP_OFF) {
        vdDisplayMessageFromFile(TRANS_NOT_ALLWD_ERR_MSG, ERROR_MSG);
        return(VS_ERR);
    }

    if (inGetPrinterType() != NOPRINTER) {
        inGetClerkTemplate();

        inPrintIRSTipReport(&obPrinter, pobTran);
    }
    return(VS_SUCCESS);
}


/*********************************************************************************************
* @func int |inPrintIRSTipReport |
*
*  Initiates printing of the IRS Tip reportort
*
*
*
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ERR
*
*
* @end
**********************************************************************************************/


int inPrintIRSTipReport(PRINTER_OBJECT* obPrinter,TRANSACTION_OBJECT* pobTran) {
    ADT_REC srADTRec;
    FHANDLE hADTFHandle;
    char szADTFileName[FILENAME_SIZE + 1];

    unsigned char ucUDTRecBuff[SIZE_UDT_REC + 1];
    unsigned char * ucUDTRec = ucUDTRecBuff;

    FHANDLE hUDTFHandle;

    UTT_REC srUTTRec;
    UTT_KEY srUTTKey;
    char szUTTFileName[FILENAME_SIZE + 1] ;
    DB_FILE srDBFPtr;

    int inRetVal;
    long lnRecNum;
    char szClerkId[CLERK_ID_SIZE + 1];

    int inPrintAll;
    int inStart,
    inStop,
    inMerch = 0;
    int inHostIndex;
    int inResult,
    inRecordSize;
    long lnCashTipAmt,
    lnTipSale;
    long lnTipAmount,
    lnTotTipSale;
    long lnTotCashTipAmt,
    lnTotTipAmt;

    int inClerkCount = 0;
    int inReportTop = VS_TRUE;

    /** /pdebug(("--inPrintIRSTipReport--")); */

    if (ifOddBoundary(ucUDTRec))   /* if on odd boundary increment pointer */
        ucUDTRec++;     
    lnCashTipAmt = 0L;
    lnTipSale = 0L;
    lnTipAmount = 0L;
    lnTotTipSale = 0L;
    lnTotCashTipAmt = 0L;
    lnTotTipAmt = 0L;

    inStart = 1;
    inStop = inGetMITTotRecs();

    if ((inPrintAll = inGetClerkSelection(szClerkId)) == VS_ESCAPE)
        return(VS_ESCAPE);

    if (obDisp.inTitleLine == obDisp.inStatusLine || obDisp.inTitleLine == obDisp.inInputLine)
        clrscr();

    if (inPrintAll == KEY_ONERPT) {
        if (fGetCashTipOn()) {
            inAdjustCashTip(szClerkId); /* get cash tip */
        }
    } /* end if one clerk */

    if (inLoadMITRec(0) != VS_SUCCESS)
        return(VS_ERR);
    inCombineActiveClerks();
    /* This is useing temp ADT file created in CombineActiveClerks  */
    vdGetMessageFromFile(ADT_FILENAME, szADTFileName);

    /* open ADT table */
    if ((hADTFHandle = open(szADTFileName, O_RDWR)) < 0) {
        vdDisplayMessageFromFile(BATCH_EMPTY_MSG, WARNING_MSG);
        SVC_WAIT(1000);
        return(VS_ERR);
    }


    do {
        if ((inRecordSize = read(hADTFHandle, (char *) &srADTRec, SIZE_ADT_REC)) == SIZE_ADT_REC) {
            lnTipSale = 0L;
            lnTipAmount = 0L;
            lnCashTipAmt = 0L;

            if ((inPrintAll == KEY_ALLRPT) || ((inPrintAll == KEY_ONERPT) && (strcmp(szClerkId, srADTRec.szClerkId) == 0))) {
                for (inMerch = inStart; inMerch <= inStop; inMerch++) {
                    if (inLoadMITRec(inMerch - 1) != VS_SUCCESS) {
                        close(hADTFHandle);
                        return(VS_ERR);
                    }

                    /* open correct merchant batch */
                    if (inGetDCBatchRecCnt(pobTran) == 0)
                        continue;

                    for (inHostIndex = 0; inHostIndex < MAX_HOSTS; inHostIndex++) {
                        //                      vdSetHostHandle(inHostIndex);

                        if (inLoadDefaultHost(inHostIndex) != VS_SUCCESS) {
                            close(hADTFHandle);
                            return (VS_ERR);
                        }
                        if (inLoadDefaultCurrency(pobTran) != VS_SUCCESS) {
                            close(hADTFHandle);
                            return (VS_ERR);
                        }

                        /* Only print the report for independent host */
                        if (!fIsIndependentBatch(inHostIndex))
                            continue;

                        vdGetUTTFileName(szUTTFileName);
                        if (inOpenUTT(&srDBFPtr, szUTTFileName) < 0)
                            continue;

                        strcpy(srUTTKey.szClerkID, srADTRec.szClerkId);

                        inRetVal = inKeyReadUTT(&srDBFPtr, &srUTTRec, &srUTTKey, KEY_SVR_ID, &lnRecNum);
                        if (inRetVal == VS_SUCCESS) {
                            /* keeps a clerk total */
                            lnTipSale += srUTTRec.lnTipSale;
                            lnTipAmount += srUTTRec.lnNetTipAmt;
                            /* keeps a grand total */
                            lnTotTipSale += srUTTRec.lnTipSale;
                            lnTotTipAmt += srUTTRec.lnNetTipAmt;
                        }
                        inCloseUTT(&srDBFPtr);
                    }/* End of Host loop */
                }/* End of Merchant loop */

                if ((hUDTFHandle = open(UDT_FILENAME, O_RDWR)) < 0) {
                    close(hADTFHandle);
                    return(VS_ERR);
                }

                if (lseek(hUDTFHandle, (long) GEN_VER_SIZE, SEEK_SET) != (long) GEN_VER_SIZE) {
                    close(hADTFHandle);
                    close(hUDTFHandle);
                    return(VS_ERR);
                }
                do {
                    inResult = read(hUDTFHandle, (char *) ucUDTRec, SIZE_UDT_REC);
                    if (inResult != (int) SIZE_UDT_REC)
                        break;

                    if (strcmp(srADTRec.szClerkId, szGetClerkIdFromStruct(ucUDTRec)) == 0) {
                        lnCashTipAmt += lnGetCashTipFromStruct(ucUDTRec);
                        lnTotCashTipAmt += lnGetCashTipFromStruct(ucUDTRec);
                        break;
                    }
                } while (1);

                close(hUDTFHandle);
            }/* End of All or One check */
            if (lnTipSale || lnCashTipAmt) {
                if (inReportTop == VS_TRUE) {
                    vdMakeReportHeader(pobTran);
                    if (inGetMITTotRecs() == 1) {
                        if (fGetEnablePrinter())
                            vdDisplayMessageFromFile(PRINTING_MSG, STATUS_MSG);
                        inPrintReptHeader(pobTran, obPrinter);
                    }
                    vdSetIRSTipHeaderCondition(VS_FALSE);
                    if (inPrintLines(obPrinter, IRS_TIP_TOP_START, IRS_TIP_TOP_END, NO_BL_LINE, lnHeaderCondition) != VS_SUCCESS) {
                        close(hADTFHandle);
                        return (VS_ERR);
                    }                    
                    inReportTop = VS_FALSE;
                }
                if (inPrintIRSTipOneClerk(obPrinter, srADTRec.szClerkId, lnTipSale, lnTipAmount, lnCashTipAmt)) {
                    close(hADTFHandle);
                    return (VS_ERR);
                }                    
                if (act_kbd_pending_test(KEY_CANCEL)) {
                    close(hADTFHandle);
                    return(VS_ESCAPE);
                }
                inClerkCount++ ;
            }
        }/* End of clerk read */
    } while (inRecordSize == (int) SIZE_ADT_REC);

    close(hADTFHandle);
    remove(ADT_FILE_NAME);

    /* if more than one clerk print grand total */
    if (inClerkCount > 1 && inReportTop != VS_TRUE) {
        if (inPrintAll == KEY_ONERPT)
            vdSetIRSTipBottomCondition();
        else {
            if (inPrintIRSGrandTotals(obPrinter, lnTotTipSale, lnTotTipAmt, lnTotCashTipAmt))
                return (VS_ERR);
            lnBottomCondition = 0x00;
        }

        if (inPrintLines(obPrinter, IRS_TIP_BOTTOM_START, IRS_TIP_BOTTOM_END, NO_BL_LINE, lnBottomCondition) != VS_SUCCESS)
            return(VS_ERR);
    }
    /* if only one clerk do page feed */
    if (inClerkCount == 1 && inReportTop != VS_TRUE) {
        if (inPrintAll == KEY_ONERPT)             /* fix it so signature line will */
            vdSetIRSTipBottomCondition();    /* print if enabled and one clrk selected */
        else
            lnBottomCondition = 0x00;

        if (inPrintLines(obPrinter, IRS_TIP_BOTTOM_START, IRS_TIP_BOTTOM_END, NO_BL_LINE, lnBottomCondition) != VS_SUCCESS)
            return(VS_ERR);
    }
    /* if no record found in the UTT or ADT record */
    if (inClerkCount == 0 && inReportTop != VS_TRUE)
        /* print Server # and No Transaction msg */ {
        vdSetIRSNoTransCondition();

        if (inPrintAll == KEY_ONERPT)
            strcpy(srRcpt.unFrm.srDet.srMid.szClerkId, szClerkId);
        else
            strcpy(srRcpt.unFrm.srDet.srMid.szClerkId, " ");

        if (inPrintLines(obPrinter, IRS_TIP_NOTRAN_START, IRS_TIP_NOTRAN_END, NO_BL_LINE, lnBottomCondition) != VS_SUCCESS)
            return(VS_ERR);
    }
    if (inReportTop == VS_TRUE) {
        vdDisplayMessageFromFile(NO_DATA_MSG, WARNING_MSG);
        SVC_WAIT(1000);
    }

    return (VS_SUCCESS);
} /* end of inPrintIRSTipReport() */


/*********************************************************************************************
* @func int |vdMakeDiscTipMiddle |
*
*  Create data for middle portion of the discount tip report
*
*
*
* @rdesc return values.
* @flag int    |  No return value
*
*                 
* @end
**********************************************************************************************/



/* #ifdef BATCH_DEFINED */

void vdMakeDiscTipMiddle(TRANSACTION_OBJECT* pobTran,DISCTIP_TOTALS* psrTot) {
    char szAmount[AMT_SIZE + 1];
    /*    int len = 0; */
    long lnTipDiscAmt,
    lnNetTipAmount,
    lnTipDisc;
    int inTransType = 0;
    char invnum[INV_NUM_SIZE + 1];

    TRANSACTION_OBJECT obTran; /* bb1 */

    /* Load TRT locally for batch record */
    inLoadTRTRec(&obTran, pobTran->srBRec.inCode); /* bb1 */

    memset(srRcpt.unFrm.srDet.srMid.szCType, 0x00, sizeof(srRcpt.unFrm.srDet.srMid.szCType));
    memset(srRcpt.unFrm.srDet.srMid.szTipAmt, 0x00, sizeof(srRcpt.unFrm.srDet.srMid.szTipAmt));
    szAmount[0] = '\0';

    if (pobTran->srBRec.lnMessage)
        vdGetMessageFromFile(pobTran->srBRec.lnMessage, srRcpt.unFrm.srDet.srMid.szTransType);

    long2str(invnum, pobTran->srBKey.lnInvNum);
    pad(srRcpt.unFrm.srDet.srMid.szInvNum, invnum, '0', inGetInvNumSize(), RIGHT);

    strcpy(srRcpt.unFrm.srDet.srMid.szCType, szGetCardAbbrev());

    lnTipDisc = lnGetTipDiscount();
    inComputePercent(lnTipDisc, szAmount, pobTran->srBRec.lnTipAmount, TIP_FORMAT); 
    lnTipDiscAmt = atol(szAmount);
    lnNetTipAmount = (pobTran->srBRec.lnTipAmount) - lnTipDiscAmt;

    if (!pobTran->srBKey.fVoided)
        sprintf(srRcpt.unFrm.srDet.srMid.szTot, SUBSTITUTE_LD, pobTran->srBKey.lnTotalOfTransactionAmount);
    else
        strcpy(srRcpt.unFrm.srDet.srMid.szTot, "000");
    vdFormatAmount(srRcpt.unFrm.srDet.srMid.szTot, VS_FALSE);
    vdTrimLeadSpaces(srRcpt.unFrm.srDet.srMid.szTot);


    if (!pobTran->srBKey.fVoided && !fRefundTrans(pobTran))
        inTransType = SALE;
    else if (fRefundTrans(pobTran))
        inTransType = REFUND;
    else
        inTransType = VOID;

    switch (inTransType) {
    case    SALE:
        psrTot->lnTipAmt += pobTran->srBRec.lnTipAmount;
        psrTot->lnNetTipTot += lnNetTipAmount;
        psrTot->lnTipTot += lnTipDiscAmt;
        psrTot->lnGrandAmt += (pobTran->srBKey.lnTotalOfTransactionAmount);
        if (pobTran->srBRec.lnTipAmount > 0L)/*(pobTran->srBRec.fTipEntered)*/ {
            sprintf(srRcpt.unFrm.srDet.srMid.szTipAmt, SUBSTITUTE_LD, pobTran->srBRec.lnTipAmount);
            sprintf(srRcpt.unFrm.srDet.srMid.szCashTipAmt, SUBSTITUTE_LD, lnNetTipAmount);
            sprintf(srRcpt.unFrm.srDet.srMid.szTipPercent, SUBSTITUTE_LD, lnTipDiscAmt);
        } else {
            strcpy(srRcpt.unFrm.srDet.srMid.szTipAmt, "000");
            strcpy(srRcpt.unFrm.srDet.srMid.szCashTipAmt, "000");
            strcpy(srRcpt.unFrm.srDet.srMid.szTipPercent, "000");
        }
        break;

    case    REFUND:
        psrTot->lnTipAmt -= pobTran->srBRec.lnTipAmount;
        psrTot->lnNetTipTot -= lnNetTipAmount;
        psrTot->lnTipTot -= lnTipDiscAmt;
        psrTot->lnGrandAmt -= pobTran->srBKey.lnTotalOfTransactionAmount; /* do not add refunds*/
        if (pobTran->srBRec.lnTipAmount > 0L)/*(pobTran->srBRec.fTipEntered)*/ {
            sprintf(srRcpt.unFrm.srDet.srMid.szTipAmt, SUBSTITUTE_LD, pobTran->srBRec.lnTipAmount);
            sprintf(srRcpt.unFrm.srDet.srMid.szCashTipAmt, SUBSTITUTE_LD, lnNetTipAmount);
            sprintf(srRcpt.unFrm.srDet.srMid.szTipPercent, SUBSTITUTE_LD, lnTipDiscAmt);
        } else {
            strcpy(srRcpt.unFrm.srDet.srMid.szTipAmt, "000");
            strcpy(srRcpt.unFrm.srDet.srMid.szCashTipAmt, "000");
            strcpy(srRcpt.unFrm.srDet.srMid.szTipPercent, "000");
        }
        break;

    default:
        break;
    }

    if (inGetTipProcessing() > TIP_OFF) {
        vdFormatAmount(srRcpt.unFrm.srDet.srMid.szTipAmt, VS_FALSE);
        vdTrimLeadSpaces(srRcpt.unFrm.srDet.srMid.szTipAmt);
        vdFormatAmount(srRcpt.unFrm.srDet.srMid.szCashTipAmt, VS_FALSE);
        vdTrimLeadSpaces(srRcpt.unFrm.srDet.srMid.szCashTipAmt);
        vdFormatAmount(srRcpt.unFrm.srDet.srMid.szTipPercent, VS_FALSE);
        vdTrimLeadSpaces(srRcpt.unFrm.srDet.srMid.szTipPercent);
    } else {
        strcpy(srRcpt.unFrm.srDet.srMid.szTipAmt, "000");
        strcpy(srRcpt.unFrm.srDet.srMid.szCashTipAmt, "000");
        strcpy(srRcpt.unFrm.srDet.srMid.szTipPercent, "000");
    }
}

/********************************************************************************************* 
* @func int |inIRSTipProc |
*  
*  Initiate IRS Tip processing - calls ingettiportax, then it saves the tip
*                 to the GDS.
* 
*
*
* @rdesc return values.
* @flag int    |  VS_ERR     : error in one of the inputs
*                 VS_SUCCESS : value stored.
*
*       
*                 
* @end
**********************************************************************************************/


int inIRSTipProc(char* szClerkId) {
    char szAmt[AMT_SIZE + 1],
    * szDisplaySymbol;
    int inResult;
    char szDisplayBuffer[DISP_STR_LEN + 1];

    /** /pdebug(("--inIRSTipProc--"));*/
    szAmt[0] = '\0';

    if (fGetCashTipOn()) {
        vdDisplayMessageFromFile(CASH_TIP_AMT_MSG, ENTRY_MSG);
        szDisplaySymbol = szGetCurSymbol();
        vdGetMessageFromFile(CASH_TIP_AMT_MSG, szDisplayBuffer);
        //      BT_TRACE ('I', szDisplayBuffer);
        if ((inResult = inGetAmount(szAmt, VS_TRUE, 0L, 999999999L, szDisplaySymbol)) != VS_SUCCESS)
            return(inResult);
    }

    inUpdateUDTRecord(szClerkId, atol(szAmt));

    return(VS_SUCCESS);
}


/********************************************************************************************* 
* @func int |nGetClerkTemplate |
*  
*  Set the report template to the active template
* 
*
*
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ERR 
*       
*                 
* @end
**********************************************************************************************/

int inGetClerkTemplate() {
    char szTemplateFileName[FILENAME_SIZE + 1];

    strcpy(szTemplateFileName, STATIC_DATA_PATH);
    strcat(szTemplateFileName, szGetClerkReportNamePrefix());
    strcat(szTemplateFileName, szGetTemplateFileNameSuffix());

    if (inSwitchTemplate(szTemplateFileName) < 0) {
        vdDisplayMessageFromFile(FORMATER_ERR_MSG, ERROR_MSG);
        return(VS_ERR);
    }
    return(VS_SUCCESS);
}        


/********************************************************************************************* 
* @func int |inAddActiveClerk |
*  
*  Add Clerk to the Active Clerk Array
*                  The clerks in this array will be used as key, in printing
*                  the server total report (Grand Total Section)
* 
*
*
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ERR 
*       
*                 
* @end
**********************************************************************************************/

int inAddActiveClerk(char szClerkId[CLERK_ID_SIZE + 1],char chShift,ACTIVE_CLERK srActiveClerk[MAX_ACTIVE_CLERK]) {
    VS_BOOL inClerkFound = VS_FALSE;
    int i,
    inRetVal;
    int inClerkIdx = -1;
    /* search for first available slot  */
    do {
        inClerkIdx++;
    } while ((strcmp(srActiveClerk[inClerkIdx].szClerkID, "\0") != 0) && (inClerkIdx < MAX_ACTIVE_CLERK));
    if (inClerkIdx == MAX_ACTIVE_CLERK) {
        vdDisplayMessageFromFile(EXCEED_ACTIVE_CLERK_MAX21, ERROR_MSG);
        return(VS_ERR);
    }
    if (chShift == '0')
        strcpy(srActiveClerk[inClerkIdx].szClerkID, szClerkId);
    else {
        for (i = 0; i < inClerkIdx; i++) {
            if ((inRetVal = strcmp(srActiveClerk[i].szClerkID, szClerkId)) == 0)
                inClerkFound = VS_TRUE;
        }
        if (inClerkFound == VS_FALSE)
            strcpy(srActiveClerk[inClerkIdx].szClerkID, szClerkId);
    }

    return(VS_SUCCESS);
}  /* end of inAddActiveClerk */



/*-------------------------------------------------------------------------
   Function :    inPrintClerkReportHeader
    Description :
    Parameters :
    Returns :
    Notes :
--------------------------------------------------------------------------*/

int inPrintClerkReportHeader(TRANSACTION_OBJECT* pobTran,PRINTER_OBJECT* obPrinter) {
    /** /pdebug(("--vdMakeReportHeader--"));*/

    if (inGetMITTotRecs() == 1)
        inPrintReceiptHeader(pobTran);       

    if (inPrintLines(obPrinter, CLERK_DATE_START, CLERK_DATE_END, NO_BL_LINE, lnHeaderCondition) != VS_SUCCESS)
        return(VS_ERR);                     
    return(VS_SUCCESS);
}
static int inPrtDetailMiddle(PRINTER_OBJECT* obPrinter,TRANSACTION_OBJECT* pobTran,DETAIL_TOTALS* psrDetTot) {
    /* For Advice Upload requests invoice number needs to be
    picked up from the batch record - Field62 will be loaded
    from the batch record before this function is called
    */
    vdMakeDetailMiddle(pobTran, psrDetTot);
    vdSetDetailMiddleCondition(pobTran);

    return(inPrintLines(obPrinter, CLERK_DETAIL_MIDDLE_START, CLERK_DETAIL_MIDDLE_END, NO_BL_LINE, lnMiddleCondition));
}


static int inPrintDetailCurrencyReport(TRANSACTION_OBJECT* pobTran,PRINTER_OBJECT* obPrinter,int* pinReportTop) {
#ifdef BATCH_DEFINED
    int inNewCurr,
    inNewClerk = VS_TRUE;
    int inResult,
    inNeedGrTot,
    inCnt = 0;
    int inNeedCurrTot,
    inCurr = 0;
    int inNumberOfRecordsinBatch;
    int inPrintDetail = VS_FALSE;
    int inStart,
    inStop,
    inMerch = 0;
    int inHostIndex;
    int inCurrencyFlag = VS_FALSE;
    int inFirstMerchant = VS_TRUE;
    unsigned char ucTempHDTRec[SIZE_HDT_REC + 1];

    /* Grand total accumulators */
    DETAIL_TOTALS srGrTot; 
    /* Currency total accumulators */
    DETAIL_TOTALS srCurrTot;      

    pdebug(("--inPrintDetailReport--"));

    for (inHostIndex = 0; inHostIndex < MAX_HOSTS; inHostIndex++) {
        if (inLoadHDTRecFromStruct(inHostIndex, ucTempHDTRec) != VS_SUCCESS) {
            vdSGErrorMessage(HDT_ERR_MSG21);
            return (VS_ERR);
        }

        if (fGetAllowMultiCurrencyFromStruct(ucTempHDTRec)) {
            inCurrencyFlag = VS_TRUE;
            break;
        }
    }

    if (fChkAllHostBatches(pobTran)) {
        vdDisplayMessageFromFile(BATCH_EMPTY_MSG, WARNING_MSG);
        return(VS_SUCCESS);
    }

    /* determine how many merchants should be printed */
    inStart = 1;
    inStop = inGetMITTotRecs();

    for (inMerch = inStart; inMerch <= inStop; inMerch++) {
        if (inLoadMITRec(inMerch - 1) != VS_SUCCESS)
            return(VS_ERR);
        strlcpy(srRcpt.szMerchantName, szGetMerchantText(), sizeof(srRcpt.szMerchantName));

        inZeroDetailTotals(&srGrTot);
        inNeedGrTot = 0;  /* bb1 */

        inNumberOfRecordsinBatch = inGetDCBatchRecCnt(pobTran);
        /* Check whether the batch is empty or not */
        if (inNumberOfRecordsinBatch != 0) {
            /* Need this for inGetDCKeyBatchRec() below */
            vdBatchDCOpen(pobTran);

            if (fGetEnablePrinter())
                vdDisplayMessageFromFile(PRINTING_MSG, STATUS_MSG);

            memset(srRcpt.unFrm.srReceipt.szSChg, 0x00, sizeof(srRcpt.unFrm.srReceipt.szSChg));

            /* AS1 - only print date/time once - fix AM53390 JA127 */
            if (inFirstMerchant) {
                vdMakeReportHeader(pobTran);
                vdSetClerkHeaderCondition(*pinReportTop, CLERK_REPORT, 1, inCurrencyFlag);
                if (*pinReportTop == VS_TRUE)
                    inPrintClerkReportHeader(pobTran, obPrinter);
                inFirstMerchant = VS_FALSE;
            }
            inNeedCurrTot = 0;  
            for (inCurr = 0; inCurr < inGetCSTTotRecs(); inCurr++) {
                if (inLoadCSTRec(inCurr) != VS_SUCCESS) {
                    vdSGErrorMessage(CST_ERR_MSG);
                    return(VS_ERR);
                }
                inCnt = 0;
                inZeroDetailTotals(&srCurrTot);
                pobTran->srBKey.inCurrencyIndex = inCurr;
                strcpy(srRcpt.unFrm.srDet.srMid.szCSymbol, szGetCurSymbol());

                inNewCurr = VS_TRUE;

                srRcpt.unFrm.srDet.srMid.inTotalTipCount = 0; /* mb1 */
                srRcpt.unFrm.srDet.srMid.lnTotalTipAmt = 0;   /* mb1 */

                /* Print online records */
                if ((inResult = inGetDCKeyBatchRec(pobTran, KEY_CURRENCY + KEY_SVR_ID, &pobTran->srBKey, DB_FIRST)) == VS_SUCCESS)
                    do {
                        if (inGGCode() != AUTH_ONLY && inGGCode() != IMAM_CARDVER) {
                            if (inNewCurr) {
                                inNewCurr = VS_FALSE;
                                /* Header condition should not print report title except at top of report */
                                vdSetClerkHeaderCondition(*pinReportTop, CLERK_REPORT, inNewClerk, inCurrencyFlag);
                                *pinReportTop = VS_FALSE;
                                inNewClerk = VS_FALSE;

                                /* AS1 - print correct clerk ID */
                                if (inGetClerkMode() == NO_CLERK)
                                    strcpy(srRcpt.szClerkId, " ");
                                else
                                    strlcpy(srRcpt.szClerkId, pobTran->srBKey.szClerkId, CLERK_ID_SIZE);

                                if (inPrintLines(obPrinter, CLERK_DETAIL_TOP_START, CLERK_DETAIL_TOP_END, NO_BL_LINE, lnHeaderCondition) != VS_SUCCESS)
                                    return(VS_ERR);
                            }

                            if (inPrtDetailMiddle(obPrinter, pobTran, &srCurrTot) != VS_SUCCESS)
                                return(VS_ERR);
                            inCnt++;

                            if (act_kbd_pending_test(KEY_CANCEL))
                                return(VS_ESCAPE);
                        }
                        inResult = inGetDCKeyBatchRec(pobTran, KEY_CURRENCY + KEY_SVR_ID, &pobTran->srBKey, DB_NEXT);
                    } while (inResult == VS_SUCCESS);
                    /* end of key */

                    /* Bottom */
                if (inCnt > 0) {
                    if (inGetCSTTotRecs() > 1 && inCurrencyFlag == VS_TRUE)
                        inNeedCurrTot++;
                    else
                        inNeedGrTot++;
                }
                inAccumDetailTotals(&srGrTot, &srCurrTot);
                srCurrTot.inVoidCnt = 0;

                if (inGetCSTTotRecs() > 1) {
                    if ((inNeedCurrTot > 0) && (inCnt > 0)) //mb1
                    {
                        /* Currency Totals */
                        inSetDetailPrintTotals(&srCurrTot);
                        vdSetDetailBottomCondition(pobTran, inPrintDetail, inNumberOfRecordsinBatch, 1, &srCurrTot);

                        if (inPrintLines(obPrinter, CLERK_DETAIL_BOTTOM_START, CLERK_DETAIL_BOTTOM_END, NO_BL_LINE, lnBottomCondition) != VS_SUCCESS)
                            return(VS_ERR);
                    }
                }
            }/* end currency for loop */
            inPrintDetail = VS_TRUE;

            /*  Always need grand totals if NO_SHIFT */
            if (inNeedGrTot > 0) {
                /*  Use default currency for host for grand totals */
                if (inLoadCSTRec(inGetCurrencyIdx()) != VS_SUCCESS) {
                    vdSGErrorMessage(CST_ERR_MSG);
                    return(VS_ERR);
                }
                /* Grand Totals */
                inSetDetailPrintTotals(&srGrTot);
                /* Param 3 indicates this is the GrandTotal bottom */
                vdSetDetailBottomCondition(pobTran, inPrintDetail, inNumberOfRecordsinBatch, 2, &srGrTot);
                if (inPrintLines(obPrinter, CLERK_DETAIL_BOTTOM_START, CLERK_DETAIL_BOTTOM_END, NO_BL_LINE, lnBottomCondition) != VS_SUCCESS)
                    return(VS_ERR);
            }

            memset(srRcpt.szErrorMessage, '\0', sizeof(srRcpt.szErrorMessage));
            memset(srRcpt.szStatusMessage, '\0', sizeof(srRcpt.szStatusMessage));

            vdCloseDCBatch(pobTran); /* bb1 */
        } /* end if inNumberOfRecordsinBatch != 0 */
    } /* merchant loop */
    return (VS_SUCCESS);
#endif
}

int inPrintClerkDetailReport(TRANSACTION_OBJECT* pobTran,PRINTER_OBJECT* obPrinter) {
    int inReportTop = VS_TRUE;
    int inGoodClerk;
    ADT_REC srADTRec;
    FHANDLE hADTFHandle;
    int inPrintAll = 0;
    char szClerkId[CLERK_ID_SIZE + 1];
    char szADTFileName[FILENAME_SIZE + 1] ;

    if ((inPrintAll = inGetClerkSelection(szClerkId)) == VS_ESCAPE)
        return(VS_ESCAPE);
    if (obDisp.inTitleLine == obDisp.inStatusLine || obDisp.inTitleLine == obDisp.inInputLine)
        clrscr();

    inCombineActiveClerks();
    /* This is useing temp ADT file created in CombineActiveClerks  */
    vdGetMessageFromFile(ADT_FILENAME, szADTFileName);

    /* open ADT table */
    if ((hADTFHandle = open(szADTFileName, O_RDWR)) < 0) {
        vdDisplayMessageFromFile(BATCH_EMPTY_MSG, WARNING_MSG);
        SVC_WAIT(1000);
        return(VS_ERR);
    }

    inGetClerkTemplate();

    inGoodClerk = read(hADTFHandle, (char *) &srADTRec, SIZE_ADT_REC);
    while (inGoodClerk == SIZE_ADT_REC) {
        if ((inPrintAll == KEY_ALLRPT) || ((inPrintAll == KEY_ONERPT) && (strcmp(szClerkId, srADTRec.szClerkId) == 0))) {
            /*  If record does not exist then go to next clerk  */

            strcpy(pobTran->srBKey.szClerkId, srADTRec.szClerkId);
            strcpy(srRcpt.unFrm.srDet.srMid.szClerkId, srADTRec.szClerkId);

            if ((inPrintDetailCurrencyReport(pobTran, obPrinter, &inReportTop)) != VS_SUCCESS) {
                close(hADTFHandle);
                return(VS_ERR);
            }
        }

        inGoodClerk = read(hADTFHandle, (char *) &srADTRec, SIZE_ADT_REC);
    } /* end clerk loop */
    if (inReportTop == VS_FALSE) {
        if (inPrintLines(obPrinter, CLERK_DETAIL_FORM_FEED, CLERK_DETAIL_BOTTOM_END, NO_BL_LINE, 0x80000000) != VS_SUCCESS) {
            close(hADTFHandle);
            return(VS_ERR);
        }
    }
    close(hADTFHandle);
    remove(ADT_FILE_NAME);
    return (VS_SUCCESS);
}

int CLRKRPTS_CODE_CHECK(char* a,char* b,short * c,char* d,unsigned char* e) {
    GET_VER(a, b, c, d, e);
    return 1;
}



#undef CLRKRPTS_C


#endif // CLERK_DEFINED


